package com.ffms.filter; /**
 * @author 从卿
 * @create 2022/3/19-9:55
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//把编码格式统一设置成utf-8
@WebFilter("/*")
public class CharacterFilter implements Filter {

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //进行强制转换
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp=(HttpServletResponse) response;
        //编码设置为utf-8格式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //放行
        chain.doFilter(request, response);
    }
}
